package cn.ht.jywrwater.module.ht.controller.admin.waterqualityalertcount;

import cn.ht.jywrwater.framework.common.pojo.CommonResult;
import cn.ht.jywrwater.framework.common.pojo.PageResult;
import cn.ht.jywrwater.framework.excel.core.util.ExcelUtils;
import cn.ht.jywrwater.framework.operatelog.core.annotations.OperateLog;
import cn.ht.jywrwater.module.ht.controller.admin.waterqualityalertcount.vo.*;
import cn.ht.jywrwater.module.ht.convert.waterqualityalertcount.WaterQualityAlertCountConvert;
import cn.ht.jywrwater.module.ht.dal.dataobject.waterqualityalertcount.WaterQualityAlertCountDO;
import cn.ht.jywrwater.module.ht.service.waterqualityalertcount.WaterQualityAlertCountService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.ht.jywrwater.framework.common.pojo.CommonResult.success;
import static cn.ht.jywrwater.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 水质统计")
@RestController
@RequestMapping("/ht/water-quality-alert-count")
@Validated
public class WaterQualityAlertCountController {

    @Resource
    private WaterQualityAlertCountService waterQualityAlertCountService;

    @PostMapping("/create")
    @Operation(summary = "创建水质统计")
    @PreAuthorize("@ss.hasPermission('ht:water-quality-alert-count:create')")
    public CommonResult<Long> createWaterQualityAlertCount(@Valid @RequestBody WaterQualityAlertCountCreateReqVO createReqVO) {
        return success(waterQualityAlertCountService.createWaterQualityAlertCount(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新水质统计")
    @PreAuthorize("@ss.hasPermission('ht:water-quality-alert-count:update')")
    public CommonResult<Boolean> updateWaterQualityAlertCount(@Valid @RequestBody WaterQualityAlertCountUpdateReqVO updateReqVO) {
        waterQualityAlertCountService.updateWaterQualityAlertCount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除水质统计")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ht:water-quality-alert-count:delete')")
    public CommonResult<Boolean> deleteWaterQualityAlertCount(@RequestParam("id") Long id) {
        waterQualityAlertCountService.deleteWaterQualityAlertCount(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得水质统计")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ht:water-quality-alert-count:query')")
    public CommonResult<WaterQualityAlertCountRespVO> getWaterQualityAlertCount(@RequestParam("id") Long id) {
        WaterQualityAlertCountDO waterQualityAlertCount = waterQualityAlertCountService.getWaterQualityAlertCount(id);
        return success(WaterQualityAlertCountConvert.INSTANCE.convert(waterQualityAlertCount));
    }

    @GetMapping("/list")
    @Operation(summary = "获得水质统计列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:water-quality-alert-count:query')")
    public CommonResult<List<WaterQualityAlertCountRespVO>> getWaterQualityAlertCountList(@RequestParam("ids") Collection<Long> ids) {
        List<WaterQualityAlertCountDO> list = waterQualityAlertCountService.getWaterQualityAlertCountList(ids);
        return success(WaterQualityAlertCountConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得水质统计分页")
    @PreAuthorize("@ss.hasPermission('ht:water-quality-alert-count:query')")
    public CommonResult<PageResult<WaterQualityAlertCountRespVO>> getWaterQualityAlertCountPage(@Valid WaterQualityAlertCountPageReqVO pageVO) {
        PageResult<WaterQualityAlertCountDO> pageResult = waterQualityAlertCountService.getWaterQualityAlertCountPage(pageVO);
        return success(WaterQualityAlertCountConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出水质统计 Excel")
    @PreAuthorize("@ss.hasPermission('ht:water-quality-alert-count:export')")
    @OperateLog(type = EXPORT)
    public void exportWaterQualityAlertCountExcel(@Valid WaterQualityAlertCountExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<WaterQualityAlertCountDO> list = waterQualityAlertCountService.getWaterQualityAlertCountList(exportReqVO);
        // 导出 Excel
        List<WaterQualityAlertCountExcelVO> datas = WaterQualityAlertCountConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "水质统计.xls", "数据", WaterQualityAlertCountExcelVO.class, datas);
    }

}
